const Common = require('../util/common')
const Constant = require('../constant/constant')
const moment = require('moment');
const LikeModel = require('../model/like')
const db = require('../db');
const { Op } = require("sequelize");
const ShareModel = require('../model/share');
const RemarkModel = require('../model/remark');
const Sequelize = require('sequelize');
const Remark = require('../model/remark');
const UserModel = require('../model/user');
let exportObj = {
    likeShare,
    cancelLikeShare,
    likeRemark,
    cancelLikeRemark,
    getUserLikeShare,
    getUserLikeRemark
};
// 导出对象，供其它模块调用
module.exports = exportObj;

//用户点赞动态
async function likeShare(req, res) {
    if (Common.check(req.body, ['s_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            return await db.transaction(async (t) => {
                const createRes1 = await LikeModel.create({
                    u_id: req.user.id,
                    s_id: req.body.s_id,
                }, { transaction: t })
                const createRes2 = await ShareModel.update({
                    like: Sequelize.literal('`like` +1')
                }, {
                    where: {
                        id: req.body.s_id,
                    }
                }, { transaction: t })
                if (createRes1.u_id && createRes2) {
                    res.json(resObj)
                } else {
                    res.json(Constant.DEFAULT_ERROR)
                }
            })
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}

//用户取消动态点赞
async function cancelLikeShare(req, res) {
    if (Common.check(req.body, ['s_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            return await db.transaction(async (t) => {
                const cancelRes1 = await LikeModel.destroy({
                    where: {
                        u_id: req.user.id,
                        s_id: req.body.s_id,
                    }
                },{transaction: t })
                const cancelRes2 = await ShareModel.update({
                    like: Sequelize.literal('`like` - 1')
                }, {
                    where: {
                        id: req.body.s_id,
                    }
                }, { transaction: t })
                if (cancelRes1 && cancelRes2) {
                    res.json(resObj)
                } else {
                    res.json(Constant.DEFAULT_ERROR)
                }
            })
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}

//用户点赞评论
async function likeRemark(req, res) {
    if (Common.check(req.body, ['r_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            return await db.transaction(async (t) => {
                const createRes1 = await LikeModel.create({
                    u_id: req.user.id,
                    r_id: req.body.r_id,
                }, { transaction: t })
                const createRes2 = await RemarkModel.update({
                    like: Sequelize.literal('`like` +1')
                }, {
                    where: {
                        id: req.body.r_id,
                    }
                }, { transaction: t })
                if (createRes1.u_id && createRes2) {
                    res.json(resObj)
                } else {
                    res.json(Constant.DEFAULT_ERROR)
                }
            })

        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }


    }
}

//用户取消评论点赞
async function cancelLikeRemark(req, res) {
    if (Common.check(req.body, ['r_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            return await db.transaction(async (t) => {
                const cancelRes1 = await LikeModel.destroy({
                    where: {
                        u_id: req.user.id,
                        r_id: req.body.r_id,
                    }
                },{transaction: t })
                const cancelRes2 = await RemarkModel.update({
                    like: Sequelize.literal('`like` - 1')
                }, {
                    where: {
                        id: req.body.r_id,
                    }
                }, { transaction: t })
                if (cancelRes1 && cancelRes2) {
                    res.json(resObj)
                } else {
                    res.json(Constant.DEFAULT_ERROR)
                }
            })
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}

//获取用户的点赞动态
async function getUserLikeShare(req, res) {
    console.log(req.query)
    if (Common.check(req.query, ['u_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            LikeModel.belongsTo(ShareModel, { foreignKey: 's_id' })
            ShareModel.belongsTo(UserModel, { foreignKey: 'u_id' })
            const result = await LikeModel.findAll({
                where: {
                    u_id: req.query.u_id,
                    s_id: {
                        [Op.not]: null,
                    }
                },
                include:{
                   model: ShareModel,
                   include:{
                       model:UserModel
                   }
                }
            })
            result.forEach((element, index) => {
                element.Share.dataValues.isLike = 1
                result[index] = element.Share
            });
            resObj.data = result
            res.json(resObj)
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}

//获取用户的点赞评论
async function getUserLikeRemark(req, res) {
    if (Common.check(req.query, ['u_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            LikeModel.belongsTo(RemarkModel, { foreignKey: 'r_id' })
            RemarkModel.belongsTo(UserModel, { foreignKey: 'u_id' })
            const result = await LikeModel.findAll({
                where: {
                    u_id: req.query.u_id,
                    r_id: {
                        [Op.not]: null,
                    }
                },
                include:{
                    model:RemarkModel,
                    where:{
                        s_id:{
                            [Op.not]: null,
                        }
                    },
                    include:{
                        model:UserModel
                    }
                }
            })
            result.forEach((element, index) => {
                result[index] = element.Remark
            });
            resObj.data = result
            res.json(resObj)
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}